package com.mx.zb.mng.ctrl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mx.zb.mng.bo.AgentLog;
import com.mx.zb.mng.dao.AgentLogMapper;
import com.mx.zb.mng.utils.AjaxResult;
import com.mx.zb.mng.utils.RestInter;
import com.mx.zb.mng.utils.context.ContextThread;
import com.mx.zb.mng.utils.db2.Page;
import com.mx.zb.mng.utils.db2.PageHelper;

/**
 * 
 * @ClassName: LogManageRestCtrl
 * @Description: 统计
 * @author 李志伟
 * @date 2017年10月25日 下午10:11:12 
 *
 */
@RestController
@RequestMapping("/a/calc")
public class LogManageRestCtrl {
	

	@Autowired
	AgentLogMapper agentLogMapper;
	@Autowired
	RestInter restInter;
	@Value("${custom.tenant.host}")
	private String tenantHost;

	@Value("${custom.tenant.tenantLogPath}")
	private String tenantLogPath;
	Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired  
    private Environment env;
	
	@Autowired
	LogManageCtrl logManageCtrl;
	
	private static final Map<String, String> templateMap = new HashMap<>();
	{
		
		templateMap.put("零售电商", "host");
		templateMap.put("技师服务", "host1");
	}
	
	
	public static String getShortName(String key) {
		return templateMap.get(key);
	}
	
	
	@RequestMapping(value= {"/agentLogs"}, method= {RequestMethod.GET})
	public AjaxResult getList() {
		AgentLog o = new AgentLog();
		String search = ContextThread.get().getSearch();
		if(search!=null&&search!="") {
			try{
				o= JSON.parseObject(search,AgentLog.class) ;
				if(o.getUpdateDate()!=null) {
					Calendar c=Calendar.getInstance();  
					c.setTime(o.getUpdateDate());
					c.add(Calendar.DATE, 1); 
					o.setUpdateDate(c.getTime());
				 }
			
				}catch(Exception e) {
					e.printStackTrace();
					o = new AgentLog();
			}
		}
//		 o.setAgentId(ContextThread.get().getAgentId());
		PageHelper.startPage();
		Page<AgentLog> pages = agentLogMapper.queryPage(o);
		
		return new AjaxResult().success(pages);
	}
	
	
	@RequestMapping(value= {"/tenLogs"}, method= {RequestMethod.GET})
	public Map<String, Object> getTenantList() {
		String search = ContextThread.get().getSearch();
        JSONObject postData = new JSONObject();  
        postData.put("search", search);
        int start=ContextThread.get().getStart();
        int length=ContextThread.get().getLength();
        String templateName="零售电商";
        if(StringUtils.hasText(search)) {
        	JSONObject js=JSONObject.parseObject(search);
        	templateName=js.getString("template");
        }
        String shortName = getShortName(templateName);
        String url= env.getProperty("custom.tenant." + shortName)+ tenantLogPath;
        JSONObject json= restInter.postJson( url+"?start="+start+"&length="+length, postData);
        JSONArray array= json.getJSONArray("data");
        List<AgentLog> list=JSON.parseArray(array.toJSONString(), AgentLog.class);
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("data",list);
        map.put("start",start);
        map.put("length",length);
        map.put("draw", ContextThread.get().getDraw());
        map.put("recordsTotal", json.getString("recordsTotal"));
        map.put("recordsFiltered", json.getString("recordsFiltered"));
        map.put("status", "1");

		return map;
	}
	
	

	//模板变了之对应的查询下拉框变化
	@RequestMapping(value= {"/getActions"}, method= {RequestMethod.GET})
	public AjaxResult getActions(@RequestParam String templateName) {
		String shortName = getShortName(templateName);
		  String url= env.getProperty("custom.tenant." + shortName);
		  JSONObject json= restInter.postJson( url + "/i/biz/tenant/tenantJsonConfig", new JSONObject());
			List<String> list=logManageCtrl.getJsonData(json.getJSONObject("data"));
		return new AjaxResult().success(list);
	}
	
	
}
